////////////////////////////////////////////////
// DESCRIPTION:
//    iMXS Emulator support
//
// Legal Notices:
//   Copyright (c) 2008, Telliam Consulting, LLC.
//   All rights reserved.
//
//   Redistribution and use in source and binary forms, with or without modification,
//   are permitted provided that the following conditions are met:
//
//   * Redistributions of source code must retain the above copyright notice, this list
//     of conditions and the following disclaimer.
//   * Redistributions in binary form must reproduce the above copyright notice, this
//     list of conditions and the following disclaimer in the documentation and/or other
//     materials provided with the distribution.
//   * Neither the name of Telliam Consulting nor the names of its contributors may be
//     used to endorse or promote products derived from this software without specific
//     prior written permission.
//
//   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
//   EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
//   OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
//   SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
//   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
//   TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
//   BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
//   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
//   ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
//   DAMAGE. 
//
using System;
using System.Security.Permissions;
using Microsoft.SPOT.Hardware;
using FusionWare.SPOT.Runtime.Serialization;

// normally these go in the AssemblyInfo.cs but that is generated for this project to get
// a build specific version number for the assembly.
// MS emulator engine does not support partially trusted components
[assembly: SecurityPermission( SecurityAction.RequestMinimum, Unrestricted = true )]

namespace FusionWare.SPOT.EmulatorComponents.iMXS
{
    /// <summary>Emulator for systems based on the Freescale iMXS CPU</summary>
    /// <remarks>
    /// This custom emulator ties all the components into a single UI form thus making for a
    /// clean user experience. 
    /// 
    /// This class provides pin mapping for symbolic names of all the CPU pins as defined in
    /// the <see cref="GetPinByName">PinMap</see> property.
    /// </remarks>
    public class iMXSEmulator : CustomEmulator
    {
        /// <summary>Creates a new iMXSEmulator Component</summary>
        public iMXSEmulator()
        {
        }

        /// <summary>Defined constants for all fixed in hardware GPIO pins</summary>
        /// <remarks>
        /// This class provides constants that symbolicly name the GPIO pins defined
        /// in the hardware for the iMXS CPU. 
        /// 
        /// It is important to use the symbolic enumerations here instead of hard
        /// coding the Cpu.Pin values as it makes things clearer and isolates the
        /// application from differences in the hardware (and the emulator) over
        /// time. 
        /// </remarks>
        public enum Pin
        {
            /// <summary>Timer Input or GPIO</summary>
            TIN = 1,
            /// <summary>PWM Output or GPIO</summary>
            ///<remarks>Currently only supported as GPIO</remarks>
            PWM = 2,
            /// <summary>I2C Bus SDA Line</summary>
            I2C_SDA = 15,
            /// <summary>I2C Bus SCL Line</summary>
            I2C_SCL = 16,
            /// <summary>UART2 Transmit</summary>
            UART2_TXD = 62,
            /// <summary>UART2 Receive</summary>
            UART2_RXD = 63,
            /// <summary>SSI Receive Frame Synch</summary>
            /// <remarks>Currently only supported as a GPIO</remarks>
            SSI_RXFS = 67,
            /// <summary>SSI Receive Clock</summary>
            /// <remarks>Currently only supported as a GPIO</remarks>
            SSI_RXCLK = 68,
            /// <summary>SSI Receive Data</summary>
            /// <remarks>Currently only supported as a GPIO</remarks>
            SSI_RXDAT = 69,
            /// <summary>SSI Transmit Data</summary>
            /// <remarks>Currentl49y only supported as a GPIO</remarks>
            SSI_TXDAT = 70,
            /// <summary>SSI Transmit Frame Synch</summary>
            /// <remarks>Currently only supported as a GPIO</remarks>
            SSI_TXFS = 71,
            /// <summary>SSI Transmit Clock</summary>
            /// <remarks>Currently only supported as a GPIO</remarks>
            SSI_TXCLK = 72,
            /// <summary>UART1 (COM1) Clear To Send</summary>
            /// <remarks>Currently Only supported as GPIO</remarks>
            UART1_CTS = 73,
            /// <summary>UART1 (COM1) Request To Send</summary>
            /// <remarks>Currently Only supported as GPIO</remarks>
            UART1_RTS = 74,
            /// <summary>UART1 (COM1) Transmit Data</summary>
            UART1_TXD = 75,
            /// <summary>UART1 (COM1) Receive Data</summary>
            UART1_RXD = 76,
            /// <summary>SPI Bus Ready</summary>
            SPI_RDY = 77,
            /// <summary>SPI Bus Clock</summary>
            SPI_SCLK = 78,
            /// <summary>SPI Bus SLave Select</summary>
            SPI_SS = 79,
            /// <summary>SPI Bus Master-In/Slave-Out</summary>
            SPI_MISO = 80,
            /// <summary>SPI Bus Master-Out/Slave-In</summary>
            SPI_MOSI = 81,
            /// <summary>UART2 (COM2) Clear To Send</summary>
            /// <remarks>Currently Only supported as GPIO</remarks>
            UART2_CTS = 92,
            /// <summary>UART2 (COM2) Clear To Send</summary>
            /// <remarks>Currently Only supported as GPIO</remarks>
            UART2_RTS = 93,
            /// <summary>LCD Shift Clock</summary>
            LSCLK = 102,
            /// <summary>LCD Reverse scan</summary>
            REV = 103,
            /// <summary>LCD Start signal output for gate driver #</summary>
            CLS = 104,
            /// <summary>LCD Control signal output for source driver #</summary>
            PS = 105,
            /// <summary>LCD Program horizontal scan direction #</summary>
            SPL_SPR = 106,
            /// <summary>LCD bias voltage control</summary>
            CONTRAST = 107,
            /// <summary>LCD Alternate crystal direction / output enable</summary>
            ACD_OE = 108,
            /// <summary>LCD Line pulse or horizontal sync</summary>
            HSYNC = 109,
            /// <summary>LCD Line pulse or vertical sync</summary>
            VSYNC = 110,
            /// <summary>LCD Data Line 0</summary>
            LCDD0 = 111,
            /// <summary>LCD Data Line 1</summary>
            LCDD1 = 112,
            /// <summary>LCD Data Line 2</summary>
            LCDD2 = 113,
            /// <summary>LCD Data Line 3</summary>
            LCDD3 = 114,
            /// <summary>LCD Data Line 4</summary>
            LCDD4 = 115,
            /// <summary>LCD Data Line 5</summary>
            LCDD5 = 116,
            /// <summary>LCD Data Line 6</summary>
            LCDD6 = 117,
            /// <summary>LCD Data Line 7</summary>
            LCDD7 = 118,
            /// <summary>LCD Data Line 8</summary>
            LCDD8 = 119,
            /// <summary>LCD Data Line 9</summary>
            LCDD9 = 120,
            /// <summary>LCD Data Line 10</summary>
            LCDD10 = 121,
            /// <summary>LCD Data Line 11</summary>
            LCDD11 = 122,
            /// <summary>LCD Data Line 12</summary>
            LCDD12 = 123,
            /// <summary>LCD Data Line 13</summary>
            LCDD13 = 124,
            /// <summary>LCD Data Line 14</summary>
            LCDD14 = 125,
            /// <summary>LCD Data Line 15</summary>
            LCDD15 = 126,
            /// <summary>Timer2 Output</summary>
            TMR2OUT = 127
        }

        /// <summary>Retrieves the pin number for a named pin</summary>
        /// <param name="Name">Name for the pin</param>
        /// <exception cref="ArgumentException">The name is not a valid one</exception>
        /// <returns>Pin number for the named pin</returns>
        public override Cpu.Pin GetPinByName(string Name)
        {
            return (Cpu.Pin)Enum.Parse(typeof(Pin), Name, true);
        }

        /// <summary>Pre-configures default components</summary>
        /// <remarks>
        /// <para>This method is Called by <see cref="CustomEmulator.LoadDefaultComponents"/>
        /// after calling <see cref="M:Microsoft.SPOT.Emulator.Emulator.LoadDefaultComponents"/> 
        /// but before auto registration of the UI components. This allows
        /// derived classes, such as this one, a chance to set up additional
        /// configuration such as the memory managers, maximum GPIO ports, System
        /// Clock frequency, etc...</para>
        /// <para>This implementation registers the VersionInfo and PWM components for pseudo
        /// "interop" via a virtual serial port. It also sets the system parameters to a set
        /// of defaults as follows:
        /// <list type="Table">
        /// <listheader><term>Name</term><description>Value</description></listheader>
        /// <item><term>base.GpioPorts.MaxPorts</term><description>128</description></item>
        /// <item><term>base.TimingServices.SystemClockFrequency</term><description>16000000</description></item>
        /// <item><term>base.MemoryManager.RamManager.Size</term><description>8MB</description></item>
        /// </list>
        /// </para>
        /// </remarks>
        protected override void PreConfigDefaultComponents()
        {
            base.PreConfigDefaultComponents();
            base.GpioPorts.MaxPorts = 128;
            base.TimingServices.SystemClockFrequency = 16000000;
            base.RamManager.Size = 8 * 1024 * 1024;
        }
    }
}
